<?php

class TempAction extends BaseAction {

    public function index() {
        //筛选
        $tempModel = M("Temps");
        $farmwork = $this->_param("farmwork");
        $trade = $this->_param("trade");
        $color = $this->_param("color");
        $sty = $this->_param("sty");
        $sort = $this->_param("sort");
        $rate = $this->_param("rate");
        $this->assign("farmwork", $farmwork);
        $this->assign("trade", $trade);
        $this->assign("color", $color);
        $this->assign("sty", $sty);
        $this->assign("sort", $sort);
        $this->assign("rate", $rate);
        
        $map = "status = 1 and ";
        if ($farmwork == 0 || $farmwork == null) { 
            $f = "farmwork >0 and  farmwork < 5 and "; //旺铺类型 新旺铺 全部 
        } elseif ($farmwork == 5) {
            $f = "farmwork > 5 and farmwork < 8 and  "; //旺铺类型 旧旺铺 全部
        } else {
            $f .= "farmwork = $farmwork and "; //旺铺筛选
        }
        if ($trade == 0 || $trade == null) {
            $t = " trade > 0 and "; //行业全部
        } else {
            $t = " trade = $trade and "; //行业筛选
        }
        if ($color == 0 || $color == null) {
            $c = " trade > 0 and "; //色系全部
        } else {
            $c = "color = $color and "; //色系筛选
        }
        if ($sty == 0 || $sty == null) {
            $s = "sty > 0 and "; //风格全部
        } else {
            $s = "sty = $sty and "; //风格筛选
        }
        if ($rate == 0 || $rate == null) {
            $r = " rate > 0  "; //返现全部
        } else {
            $r = "rate = " . $rate / 100; //返现筛选
        }
        if ($sort == 0 || $sort == null) {
            $so = " rate desc,count desc,ftime desc "; //默认排序
        } elseif ($sort == 1) {
            $so = "price asc"; //价格升序
        } elseif ($sort == 2) {
            $so = "price desc";//价格降序
        } elseif ($sort == 3) {
            $so = "ftime desc";//上架时间降序
        } elseif ($sort == 4 || $sort == 5) {
            $so = "count desc"; //淘宝销量降序
        } elseif ($sort == 7) {
            $so = "price desc"; //价格降序
        } elseif ($sort == 8) {
            $so = "price asc"; //价格升序
        }

        $key = "status = 1";
        $temps = $tempModel->where($map . $f . $t . $c . $s . $r." and num > todaysale")->select();
        import('ORG.Util.Page'); // 导入分页类
        $count = count($temps); // 查询满足要求的总记录数
        $Page = new Page($count, 15);
        $show = $Page->show(); // 分页显示输出
        $list = $tempModel->where($map . $f . $t . $c . $s . $r." and num > todaysale")->order($so)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign("temps", $list);
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }
    //模板详情页
    public function detail() {
        $id = $this->_param("id");
        $tempModel = M("Temps");
        $type = $tempModel->where("id = $id")->getField("type");
        if ($type == 1)
            $url = "http://zxn.taobao.com/detail.htm?id=" . $id;
        else
            $url = "http://zx.taobao.com/detail.htm?id=" . $id;
        $html = file_get_contents($url);
        $html = preg_replace("/\s/", "", $html);
        $html = iconv("gb2312", "utf-8//IGNORE", $html);
        $pa = "/<dt>总销售量：<\/dt><dd><strong>(\d+)<\/strong>/"; //获取淘宝销量
        preg_match_all($pa, $html, $match);
        $html = $match[1];
        $temp = $tempModel->select($id);
        $this->assign("html", $html[0]);
        $this->assign("temp", $temp[0]);
        $this->display();
    }

    public function qhtml() {
        //获取详情页
        $id = $this->_param("id");
        $tempModel = M("Temps");
        $temp = $tempModel->select($id);
        if ($temp[0]['type'] == 1)
            $url = "http://zxn.taobao.com/detail.htm?id=" . $temp[0]['id'];
        else
            $url = "http://zx.taobao.com/detail.htm?id=" . $temp[0]['id'];
        $html = file_get_contents($url); //匹配模板详情页
        $html = iconv("gb2312", "utf-8//IGNORE", $html);
        $pa = '%<div class="tab-content.*?>(.*?)<div style="display: none;" class="hide comment clear" role="tabpanel" aria-hidden="true" aria-labelledby="ks-switchable8">%si';
        preg_match_all($pa, $html, $match);
        $html = ($match[1]);
        echo $html[0];
    }

    //点击购买
    public function buy() {
        $rid = rand("100", "999");
        $id = $this->_param("id"); //模板id
        $nameid = $this->_param("nameid"); //nameid
        $user = M("users");
        $temps = M("temps");
        $name = $user->where("id = $nameid")->getField("name"); //获取模板用户名
        $title = $temps->where("id = $id")->getField("title"); //获取模板标题
        $ind = date("YmdHis");
        $order = M("orders");
        //生成订单
        $data['name'] = $_SESSION['name'];
        $data['tempid'] = $id;
        $data['title'] = $title;
        $data['fname'] = $name;
        $data['indent'] = $ind;
        $data['buyer'] = 0;
        $data['date'] = date("Y-m-d H:i:s");
        $data['status'] = 1;
        $order->add($data);
        echo '<div id="g" class="tg" ><div class="ce"><h3>是否完成购买</h3><div class="in">请输入购买的旺旺账号:<input type="text" id="wwname" /></div><div class="tx"><a href="javascript:;" onclick="goy(1,' . $ind . ')" >已完成购买</a><a href="javascript:;" onclick="goy(0)">取消购买</a></div></div></div>';
    }

    //购买确认
    public function buyy() {
        $ind = $this->_param("ind");
        $wwname = $this->_param("wwname");
        $order = M("orders");
        $tem = M("temps");
        $data['status'] = 2; //更改状态
        $data['wwname'] = $wwname; 
        $order->where("indent = $ind")->save($data);
    }

}

?>